#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ctds01.py 
@version:
@time: 2022/01/06 
"""
import allure

import db_info
from TapClientCore.T06_Tapdata_feature.v1.TPF01_datasource.tpf_datasource_v1 import TPFV1DataSource


class V1CTDataSource(TPFV1DataSource):

    def _update_to_init_info(self, data):
        with allure.step('update数据源'):
            connection_id = self.update_datasource_by_db_info(data, {})

    # create update-init test load
    def V1_DATA_SOURCE_CREATE_TEST(self, data):
        with allure.step('创建数据源'):
            connection_id = self.create_datasource_by_db_info(data)
            self._update_to_init_info(data)
        with allure.step('测试并加载schema'):
            self.datasource_test_schema_by_db_info(data)
        return connection_id

    # create update-init load
    def V1_DATA_SOURCE_CREATE_LOAD_SCHEMA(self, data):
        with allure.step('创建数据源'):
            connection_id = self.create_datasource_by_db_info(data)
            self._update_to_init_info(data)
        with allure.step('加载schema'):
            self.load_datasource_schema_by_db_info(data)
        return connection_id

    def V1_DATA_SOURCE_DELETE(self, data):
        with allure.step('删除数据源'):
            self.delete_datasource_by_db_info(data)

    # copy test load load update delete
    def V1_DATA_SOURCE_MORE_STEP(self, data):
        with allure.step('copy数据源'):
            copy_data = self.copy_datasource_by_db_info(data)
        with allure.step('测试并加载schema'):
            self.datasource_test_schema_by_db_info(copy_data)
        with allure.step('加载schema'):
            self.load_datasource_schema_by_db_info(copy_data)
        with allure.step('update数据源'):
            new_name = copy_data.get('name') + '_please_not_use_create_mission'
            update_name = {'name': new_name}
            self.update_datasource_by_db_info(copy_data, update_name)
            copy_data.update(update_name)
        with allure.step('删除数据源'):
            self.delete_datasource_by_db_info(copy_data)


if __name__ == '__main__':
    T = V1CTDataSource()
    T.V1_DATA_SOURCE_CREATE_LOAD_SCHEMA(db_info.Mysql.mysql_source_test)
